/*
 * Licensed to Nanjing RedOrange Co. ltd (RedOrange) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * RedOrange licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package redora.util;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXParseException;

/**
 * Schema validation error handle.
 *
 * @author rick
 *
 */
public class DefaultErrorHandler implements ErrorHandler {

    private static final Logger l = Logger
            .getLogger("redora.util.DefaultErrorHandler");

    private Element errors;

    public DefaultErrorHandler() {
        this.errors = DocumentHelper.createElement("errors");
    }

    public DefaultErrorHandler(Element errors) {
        this.errors = errors;
    }

    /**
     * @return the errors
     */
    public Element getErrors() {
        return errors;
    }

    /**
     * @param errors
     * the errors to set
     */
    public void setErrors(Element errors) {
        this.errors = errors;
    }

    /**
     * add exception.
     */
    protected void addException(Element element,
            SAXParseException exception) {
        element.addAttribute("column", Integer.toString(exception
                .getColumnNumber()));
        element.addAttribute("line", Integer
                .toString(exception.getLineNumber()));

        String publicId = exception.getPublicId();
        if (publicId != null && publicId.length() > 0) {
            element.addAttribute("publicId", publicId);
        }

        String systemId = exception.getSystemId();
        if (systemId != null && systemId.length() > 0) {
            element.addAttribute("systemId", systemId);
        }

        element.addText(exception.getMessage());
   }

    /**
     * (non-Javadoc).
     *
     * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
     */
    public void error(SAXParseException exception) {
        Element element = errors.addElement("errors");
        addException(element, exception);
        l.log(Level.FINE, "XML file is invalid: " + errors.asXML());
    }

    /**
     * (non-Javadoc).
     *
     * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.
     * SAXParseException)
     */
    public void fatalError(SAXParseException exception) {
        Element element = errors.addElement("fatalError");
        addException(element, exception);
        l.log(Level.FINE, "XML file is invalid: " + errors.asXML());
    }

    /**
     * (non-Javadoc).
     *
     * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
     */
    public void warning(SAXParseException exception) {
        Element element = errors.addElement("warning");
        addException(element, exception);
        l.log(Level.FINE, "XML file is invalid: " + errors.asXML());
    }
}
